package com.hz.sys.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
 
/**   
 * @Title: AccessFilter.java 
 * @Description: TODO(设置其他IP地址的机器可以直接访问本项目Url--工具filter) 
 */
public class AccessFilter extends HttpServlet implements Filter{
    private final Logger logger=LoggerFactory.getLogger(AccessFilter.class);
    /**
     * 序列ID，用来监测控制版本更新的
     */
    private static final long serialVersionUID = 1L;
    
    @Value("${test.mavenspring.project.allow.url}")
    private String projectDemoAllowOriginUrl;
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {               
    }
 
    //设置其他IP地址的机器可以直接访问到这个项目的后端
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       // logger.info("************************跨域请求************************");
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setHeader("Access-Control-Allow-Origin", projectDemoAllowOriginUrl);
        httpResponse.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        chain.doFilter(request, httpResponse);
                
    }
 
}
